package com.li.stack;

public class ArrayStackdemo2 {
    public static void main(String[] args) {
        LinckStack<String> ns = new LinckStack<String>();
        ns.push("asdwqewqewqea");
        ns.push("ewewwqsdsadsd");
        ns.push("ffddsfsdfdfdf");
        int s = ns.size();
        System.out.println(s);
        System.out.println(ns.pop());
        System.out.println(ns.pop());
        System.out.println(ns.pop());
    }

}

class Node<T> {
    public T data;
    public Node<T> next;

    public Node() {
        // 调用有两个参数的构造方法Node(Object data,Node next)
        this(null, null);
    }

    public Node(T data) {
        // 同理
        this(data, null);
    }

    public Node(T data, Node<T> next) {
        this.data = data;
        this.next = next;
    }


}

class LinckStack<T> {
    private Node<T> top;

    public boolean isempty() {
        return top == null;
    }

    public void push(T x) {
        Node<T> temp = new Node<T>(x);
        temp.next = top;
        top = temp;
    }

    public T peek() {
        if (!isempty())
            return top.data;
        else return null;
    }

    public T pop() {
        if (isempty())
            return null;
        else {
            Node<T> temp = top;
            top = top.next;
            return temp.data;
        }
    }

    public int size() {
        Node<T> temp = top;
        int size = 0;
        while (temp != null) {
            temp = temp.next;
            ++size;
        }
        return size;
    }
}